from repoze.bfg.chameleon_zpt import get_template
from models import AVAILABLE_TYPES
MAIN_TEMPLATE = get_template("templates/main_template.pt")

#
#  A few notes on itemid
#   - the id is a string concatenation of:
#       * lat
#       * lon
#       * itemtype
# 

# Should put this in the model really
def getCommentsForItem(context, request):
    """ Return a list of comments for a given item """
    print "getting comments for an item of "
    return []

# Should put this in the model really
def getRatingsForItem(context, request):
    """ return the ratings for a given item """
    item_id = str(request.params['item_id'])
    ratings = context.getItemMetaRatings(itemId=item_id)
    if not ratings:
        # Return an empty thing
        return {'accup': 0, 'accdown': 0, 'nicetotal': 0, 'nicedown': 0, 'niceup': 0, 'acctotal': 0}
    result = ratings['obj'].getFormattedRatings()
    return result

#
# ALL views should call this to get the start of their response
# 
def _startResponse(context, request):
    response = {
        'main_template': MAIN_TEMPLATE,
        'project': 'Rewired State Hackday'
    }
    return response

def my_view(context, request):
    response = _startResponse(context, request)
    response["available_types"] = AVAILABLE_TYPES
    print "ratings:", context._metaratings
    for item in context._metaratings:
        print item['obj'].getFormattedRatings()
    return response

def osm_view(context, request):
    response = _startResponse(context, request)
    # Look up some stuff here
    
    return response

def searchASynch(context, request):
    # Searches some things and returns a list of item coordinate objects
    chose = str(request.params['selected'])
    #bounds = str(request.params['bounds'])
    #print "Boundary is:", bounds
    search = "all"
    print "Searching on:", chose
    for item in AVAILABLE_TYPES:
        if item["title"] == chose:
            search = item['name']
    # Leave out the selected bit now, because I am working the names wrong at the moment
    results = context.performSearch(request.params['lat'], request.params['lon'], search)
    return results

def addRating(context, request):
    """ ASynchronous request mechanism for adding ratings """
    item_id = str(request.params['item_id'])
    which = str(request.params['which'])
    rating = str(request.params['rating'])
    # rating, rating_type, item=None, itemId=None
    result = context.addItemMetaRating(rating, which, itemId=item_id)
    return result

def addComment(context, request):
    """ ASynchronous request mechanism for adding comments """
    return True




